/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    var menu_treegrid = $("#menu_treegrid");
    var menu_dialog = $("#menu_dialog");
    var menu_form = $("#menu_form");
    menu_treegrid.treegrid({
        url: '/menu/selectRootMenu.do',
        idField:'id',
        treeField:'text',
        fitColumns: true,
        fit: true,
        singleSelect: true,
        toolbar: '#tb',
        columns: [[
            {field: 'text', title: '菜单名称', width: 100},
            {field: 'url', title: '菜单路径', width: 100},
            {
                field: 'parent', title: '父菜单', width: 100, formatter: function (value) {
                return value ? value.text : "";
            }
            },
        ]]
    })
    menu_dialog.dialog({
        width: 300,
        height: 280,
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            menu_form.form('clear');
        }
    })

    var methodObj = {
        add: function () {
            //设置标题
            menu_dialog.dialog('setTitle', '新增菜单');
            //打开弹出框
            menu_dialog.dialog('open');
            $("#parentmenu_combotree").combotree({
                url:'/menu/selectRootMenu.do'
            })
        },
        edit: function () {

            var row = menu_treegrid.treegrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            if (row.parent) {
                row["parent.id"] = row.parent.id;
            }
            $("#parentmenu_combotree").combotree('reload');
            //把数据回显到表单中
            menu_form.form('load', row);
            //设置标题
            menu_dialog.dialog('setTitle', '编辑菜单');
            //打开弹出框
            menu_dialog.dialog('open');
            $("#parentmenu_combotree").combotree({
                url:'/menu/selectRootMenu.do'
            })

        },
        reload: function () {
            menu_treegrid.treegrid('reload');
        },
        save: function () {
            //提交ajax
            menu_form.form('submit', {
                url: '/menu/saveOrUpdate.do',
                onSubmit:function (param) {
                var parentId = $("#parentmenu_combotree").combotree('getValue');
                param['parent.id'] = parentId;
              },
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            menu_treegrid.treegrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'warning');
                    }
                }
            })
        },
        remove: function () {
            var row = menu_treegrid.treegrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if(r){
                    $.get('/menu/delete.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                menu_treegrid.treegrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        cancel: function () {
            menu_dialog.dialog('close');
        },
    }
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
